﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Model;
using DBUtility;

namespace DAL
{
    public class StockWarn : BaseRepository<StockWarnInfo>
    {   
        #region 静态变量定义部分
        private const string PARM_ID = "@ID";
        private const string PARM_TOOLCODE = "@Tool_Code";
        private const string PARM_AMOUNTWARN = "@Amount_Warn";

        private const string PARM_ROOMNAME = "@RoomName";
        #endregion

        public override StockWarnInfo LoadEntity<T1>(T1 Id)
        {
            SqlParameter param = GetParameter();
            param.Value = Id;
            return ExcuteSqlToGetEntity("tb_StockWarn_GetbyID", param);
        }

        public override IList<StockWarnInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("tb_StockWarn_GetAll", null);
        }

        public IList<StockWarnInfo> StockWarn_GetbyMultiFact(string roomName, string toolCode)
        {
            SqlParameter[] Parms = GetMultiFactParameters();
            Parms[0].Value = roomName;
            Parms[1].Value = toolCode;
            IList<StockWarnInfo> Entities = new List<StockWarnInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "tb_StockWarn_GetbyMultiFact", Parms))
            {
                while (rdr.Read())
                {
                    StockWarnInfo Entity = new StockWarnInfo
                    {
                        ID = rdr.GetValue(0).Equals(DBNull.Value) ? (int?)null : rdr.GetInt32(0),
                        Tool_Code = rdr.GetValue(1).ToString(),
                        Amount_Warn = rdr.GetValue(2) == DBNull.Value ? (int?)null : rdr.GetInt32(2),
                        RoomName = rdr.GetValue(3).ToString(),
                        Work_Shops = rdr.GetValue(4).ToString(),
                        Amount = rdr.GetInt32(5),
                    };
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }


        #region 重写基类部分
        public override string GetInsertsp()
        {
            return "tb_StockWarn_Insert";
        }

        public override string GetUpdatesp()
        {
            return "tb_StockWarn_Update";
        }

        public override string GetDeletesp()
        {
            return "tb_StockWarn_Delete";
        }

        public override StockWarnInfo GetEntityRecord(SqlDataReader rdr)
        {
            StockWarnInfo cinfo = new StockWarnInfo
            {
                ID = rdr.GetInt32(0),
                Tool_Code = rdr.GetValue(1).ToString(),
                Amount_Warn = rdr.GetInt32(2),
            };
            return cinfo;
        }

        public override StockWarnInfo GetEntityRecord()
        {
            StockWarnInfo cinfo = new StockWarnInfo();
            return cinfo;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter param = new SqlParameter(PARM_ID, SqlDbType.Int, 4);
            return param;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_ID, SqlDbType.Int, 4),
                    new SqlParameter(PARM_TOOLCODE, SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_AMOUNTWARN, SqlDbType.Int,4)
                };
            return parms;
        }

        public override void SetParameters(StockWarnInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ID;
            Parms[1].Value = Entity.Tool_Code;
            Parms[2].Value = Entity.Amount_Warn;
        }
        #endregion

        public SqlParameter[] GetMultiFactParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_ROOMNAME, SqlDbType.NVarChar, 10),
                    new SqlParameter(PARM_TOOLCODE, SqlDbType.NVarChar,50)
                };
            return parms;
        }
    }
}
